package controller

import (
	"context"
	"mldong/api/base"
	rbacApi "mldong/api/rbac"
	userApi "mldong/api/user"
	"mldong/internal/modules/sys/logic"
	"mldong/internal/modules/sys/service"
	"mldong/utility"

	"github.com/gogf/gf/v2/frame/g"
)

func init() {
	// 注册控制器时自动注入依赖
	utility.RegisterController(&RbacController{
		rbacService: utility.MustResolve(service.RbacServiceType).(*logic.RbacLogic),
	})
}

type RbacController struct {
	rbacService service.IRbacService
}

// 添加角色菜单关联
func (ctrl *RbacController) SaveRoleMenu(ctx context.Context, req *rbacApi.SaveRoleMenuReq) (res *base.CommonResult, err error) {

	// 手动绑定请求体到数组对象
	var roleMenus []rbacApi.RoleMenuParam
	if err = g.RequestFromCtx(ctx).Parse(&roleMenus); err != nil {
		return nil, err
	}
	_, err = ctrl.rbacService.SaveRoleMenu(ctx, roleMenus)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Ok())
	return
}

// 角色ID获取菜单ID集合
func (ctrl *RbacController) RoleMenuIds(ctx context.Context, req *rbacApi.RoleMenuIdsReq) (res *rbacApi.RoleMenuIdsRes, err error) {

	jsonData, err := ctrl.rbacService.RoleMenuIds(ctx, req.RoleId)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}

// 获取角色菜单列表
func (ctrl *RbacController) RoleMenuList(ctx context.Context, req *rbacApi.RoleMenuListReq) (res *rbacApi.RoleMenuListRes, err error) {

	jsonData, err := ctrl.rbacService.RoleMenuList(ctx, req.RoleId)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}

// 添加用户角色关联
func (ctrl *RbacController) SaveUserRole(ctx context.Context, req *rbacApi.SaveUserRoleReq) (res *base.CommonResult, err error) {

	// 手动绑定请求体到数组对象
	var userRoles []rbacApi.UserRoleParam
	if err = g.RequestFromCtx(ctx).Parse(&userRoles); err != nil {
		return nil, err
	}
	_, err = ctrl.rbacService.SaveUserRole(ctx, userRoles)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Ok())
	return
}

// 删除用户角色关联
func (ctrl *RbacController) RemoveUserRole(ctx context.Context, req *rbacApi.RemoveUserRoleReq) (res *base.CommonResult, err error) {

	// 手动绑定请求体到数组对象
	var userRoles []rbacApi.UserRoleParam
	if err = g.RequestFromCtx(ctx).Parse(&userRoles); err != nil {
		return nil, err
	}
	_, err = ctrl.rbacService.RemoveUserRole(ctx, userRoles)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Ok())
	return
}

// 通过角色ID获取用户列表
func (ctrl *RbacController) UserListByRoleId(ctx context.Context, req *rbacApi.UserListByRoleIdReq) (res *userApi.PageRes, err error) {
	// 传递以m_开头的查询参数
	req.QueryParamMap = utility.GetQueryParamMap(ctx)
	req.PageReq.RoleId = req.RoleId
	jsonData, err := ctrl.rbacService.UserListByRoleId(ctx, &req.PageReq)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}

// 获取用户列表-排除指定角色
func (ctrl *RbacController) UserListExcludeRoleId(ctx context.Context, req *rbacApi.UserListExcludeRoleIdReq) (res *userApi.PageRes, err error) {
	// 传递以m_开头的查询参数
	req.QueryParamMap = utility.GetQueryParamMap(ctx)
	req.PageReq.RoleId = req.RoleId
	jsonData, err := ctrl.rbacService.UserListExcludeRoleId(ctx, &req.PageReq)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}
